#include "Animation.h"

Animation::Animation()
{
    // placeholder
}

Animation::Animation (Image *pSourceImage, Vector2 frames,
                      Vector2 frameSize, int numFrames, int delay)
{
    this->pSourceImage = pSourceImage;
    this->frames = frames;
    this->frameSize = frameSize;

    this->numFrames = numFrames;
    this->delay = delay;
    frameNumber = 0;
    timeWaited = 0;
}


void Animation::DrawFrame(int frameNumber, Vector2 position)
{
//     RectangleWH areaToDraw = RectangleWH((frameNumber % framesX) * frameSizeX,
//                                            (frameNumber / framesX) * frameSizeY,
//                                         frameSizeX,
//                                            frameSizeY);
   int x = (frameNumber % int(frames.GetX())) * frameSize.GetX();
   int y = (frameNumber / int(frames.GetX())) * int(frameSize.GetY());
   int w = frameSize.GetX();
   int h = frameSize.GetY();
   RectangleWH area = RectangleWH(x, y, w, h);


     //RectangleWH areaToDraw = RectangleWH(0, 0, 400, 400);
     pSourceImage->Draw(position, area, false, false, 1, Color(1, 1, 1, 1));
}

void Animation::Update(int delta)
{
    timeWaited += delta;
    if (timeWaited >= delay)
    {
        timeWaited -= delay;
        IncrementFrame();
    }
}

void Animation::IncrementFrame(int incrementAmount)
{
    frameNumber += incrementAmount;
    if (frameNumber >= numFrames)
    {
        frameNumber -= numFrames;
    }
}

void Animation::Draw(Vector2 position)
{
    DrawFrame(frameNumber, position);
}
